package com.vmss.cspider;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.vmss.cspider.dao.DBUtil;

public class CPICInsurance {
	
	static String simpleUrl = "http://www.cpic.com.cn/cpicweb/view/customer/personal/vehicle/SimpleChinaFastCalculationViewNew.jsp";
	static String simplePattern = "\"([^\"]+)\":([^,]+),";
	
	//TODO province 可能用Map + Array 来存储
	static int citycodeArr[] = new int[]{
			110100,
			//120000,
			//.....
	};
	static int provinceCodeArr[] = new int[]{
			110000,
	};
	static int seatCountArr[] = new int[]{
		2,4,5,6,7,
	};
	
	/**
	 * 抓取
	 * 
	 * @param formRequest
	 * @return String
	 * @throws Exception
	 */
	public String getResult( Map <String, Object> formMap ) throws Exception
	{
		StringBuffer pStringBuffer = null;
		if( formMap != null && formMap.size()>0 )
		{
			pStringBuffer = NetUtils.requestPostForm(simpleUrl, formMap);
		}
		return pStringBuffer.toString();
	}
	
	/**
	 * 保存到数据库
	 * @param map
	 * @return
	 */
	public boolean saveToDB(Map <String, Object> map)
	{
		
		map.remove("_action");
		map.remove("contractExpiredMonth");
		map.remove("isCpic");
		map.remove("mobile");
		map.remove("registerDate");
		map.remove("licenseNo");
		
		try {
			DBUtil.insertToDB(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
	}
	
	
	
	/**
	 * 太平洋车险抓取执行入口
	 * @throws Exception
	 */
	public void startCatch() throws Exception
	{	
		Map <String, Object> formMap;
		for( int p=0;p<provinceCodeArr.length ;p++ )
		{
			for( int c=0;c<citycodeArr.length;c++)
			{
				for(int s=0; s<seatCountArr.length;s++)
				{
					for(int m=1;m<100;m++)
					{
						//代码恶心但是没什么好办法
						//新车无理赔历史		
						formMap = new HashMap<String, Object>();
						formMap.put("provinceCode", provinceCodeArr[p]);
						formMap.put("cityCode", citycodeArr[c]);
						formMap.put("isNewCar", 1);
						formMap.put("seatCout", seatCountArr[s]);
						formMap.put("vehiclePrice", m);
						formMap.put("claim", 0);
						
						//查询条件 非数据库存储
						formMap.put("_action", "fastCalculation");
						formMap.put("contractExpiredMonth", "2013-04-18");
						formMap.put("isCpic", 0);
						formMap.put("mobile", "13150898259");
						formMap.put("registerDate", "2012-04-01");
						
						//抓取
						String result = this.getResult(formMap);
						//匹配
						this.matcherResult(result, formMap);
						
						//旧车的理赔情况
						for( int claimCount = -5;claimCount<=3; claimCount++)
						{
							formMap = new HashMap<String, Object>();
							formMap.put("provinceCode", provinceCodeArr[p]);
							formMap.put("cityCode", citycodeArr[c]);
							formMap.put("isNewCar", 0);
							formMap.put("seatCout", seatCountArr[s]);
							formMap.put("vehiclePrice", m);
							formMap.put("claim", claimCount);
							
							//查询条件 非数据库存储
							formMap.put("_action", "fastCalculation");
							formMap.put("contractExpiredMonth", "2013-04-18");
							formMap.put("isCpic", 0);
							formMap.put("mobile", "13150898259");
							formMap.put("registerDate", "2012-04-01");
							//旧车需要车牌号码
							formMap.put("licenseNo", "京L88617");
							
							//抓取
							result = this.getResult(formMap);
							//匹配
							this.matcherResult(result, formMap);
						}
					}
					
					
				}
			}
		}
	}
	
	/**
	 * 正则匹配抓取结果并且插入数据库
	 * 一次抓取获得3个产品
	 * 
	 */
	public void matcherResult( String mString , Map <String, Object> formMap )
	{
		System.out.println(mString);
		Pattern pattern = Pattern.compile(simplePattern);
		Matcher matcher = pattern.matcher(mString);
		int realLength = 0;
		double priceArr[] = new double[ 30 ];
		Map < String, Object>pMap = null;
		
		while( matcher.find())
		{	
			if(matcher.groupCount() == 2)
			{
				//String s1 = matcher.group(1);  //描述：大众第三者责任险
				//String s2 = matcher.group(2);  //花费：924.0
				//System.out.print(s1);
				//System.out.println(s2);
				priceArr[realLength++] = Double.parseDouble(matcher.group(2));
			}
		}
		
		//如果正则的结果长度为29+1 ( 有一个是空的)  则可认为是 匹配正确
		System.out.println("=====" + realLength);
		if( realLength == 30  )
		{
			//不得已而为之
			//大众型
			pMap = new  HashMap < String, Object> ();
			pMap.put("third_response", priceArr[0]);
			pMap.put("car_damage", priceArr[1]);
			pMap.put("driver_response", priceArr[2]);
			pMap.put("passager_response", priceArr[3]);
			pMap.put("robbery_loss", priceArr[4]);
			pMap.put("third_nodedutible", priceArr[5]);
			pMap.put("robbery_nodedutible", priceArr[6]);
			//pMap.put("paint_single", priceArr[0]);
			pMap.put("car_damage_nodedutible", priceArr[7]);
			pMap.put("person_nodedutible", priceArr[8]);
			//pMap.put("paint_single_nodedutible", priceArr[0]);
			pMap.put("all_price", priceArr[9]);
			
			//合并准备插入数据库
			pMap.putAll( formMap );
			this.saveToDB(pMap);
			
			//豪华
			pMap = new  HashMap < String, Object> ();
			pMap.put("third_response", priceArr[10]);
			pMap.put("car_damage", priceArr[11]);
			pMap.put("driver_response", priceArr[12]);
			pMap.put("passager_response", priceArr[13]);
			pMap.put("robbery_loss", priceArr[14]);
			pMap.put("third_nodedutible", priceArr[15]);
			pMap.put("robbery_nodedutible", priceArr[16]);
			pMap.put("paint_single", priceArr[17]);
			pMap.put("car_damage_nodedutible", priceArr[18]);
			pMap.put("person_nodedutible", priceArr[19]);
			pMap.put("paint_single_nodedutible", priceArr[20]);
			pMap.put("all_price", priceArr[21]);
			//合并准备插入数据库
			pMap.putAll( formMap );
			this.saveToDB(pMap);
			
			//简易型
			pMap = new  HashMap < String, Object> ();
			pMap.put("third_response", priceArr[21]);
			pMap.put("car_damage", priceArr[22]);
			pMap.put("driver_response", priceArr[23]);
			pMap.put("passager_response", priceArr[24]);
			//pMap.put("robbery_loss", priceArr[0]);
			pMap.put("third_nodedutible", priceArr[25]);
			//pMap.put("robbery_nodedutible", priceArr[0]);
			//pMap.put("paint_single", priceArr[0]);
			pMap.put("car_damage_nodedutible", priceArr[26]);
			pMap.put("person_nodedutible", priceArr[27]);
			//pMap.put("paint_single_nodedutible", priceArr[0]);
			pMap.put("all_price", priceArr[28]);
			//合并准备插入数据库
			pMap.putAll( formMap );
			this.saveToDB(pMap);
		}
	}
}
